package com.ddx.controller.core;

import com.ddx.common.exception.ExceptionUtil;
import com.ddx.common.exception.ResultModel;
import com.ddx.pojo.ShoppingComment;
import com.ddx.service.IShoppingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: ${DESCRIPTION}
 * @date 2019/2/17
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "/app/shop")
public class ShoppingController {
    private static final Logger logger = LoggerFactory.getLogger(ShoppingController.class);
    @Autowired
    IShoppingService iShoppingService;
    /**
     * 添加评论
     * @param request
     * @param shoppingComment
     * @return
     */
    @RequestMapping(value = "/addComment")
    public ResultModel addComment(HttpServletRequest request, @RequestBody ShoppingComment shoppingComment) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            Integer userId = Integer.parseInt(request.getHeader("userId"));
            shoppingComment.setUserId(userId);
            iShoppingService.addComment(shoppingComment);
        } catch (Exception e) {
            logger.error("addComment fail,", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    /**
     * 修改图片
     * @param request
     * @return
     */
    @RequestMapping(value = "/uploadAvatar")
    public ResultModel uploadAvatar(HttpServletRequest request) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            String userId = request.getHeader("userId");
            String base64Avatar = request.getParameter("base64Avatar");
            iShoppingService.uploadAvatar(base64Avatar,userId);
        } catch (Exception e) {
            logger.error("uploadAvatar fail,", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
}
